package com.kpmg.datalake.common.utils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Alex.zhao [alex.zhao@kpmg.com]
 * @since Created on azhao6 on 2018-08-06 14:16:27
 */
public class BrowserUtil {
    /**
     * 静态方法，防止实例化
     */
    private BrowserUtil() {
    }

    /**
     * 通过request 获取浏览器类型
     *
     * @param request 请求头
     * @return 浏览器类型
     */
    public static String getBrowserName(HttpServletRequest request) {
        String agent = request.getHeader("User-Agent").toLowerCase();
        for (BrowserTypeEnum browserTypeEnum : BrowserTypeEnum.values()) {
            if (agent.contains(browserTypeEnum.getBrowserAgent())) {
                return browserTypeEnum.getBrowserName();
            }
        }
        return BrowserTypeEnum.OTHERS.getBrowserName();
    }

    public enum BrowserTypeEnum {
        /***
         *IE7
         */
        IE7("ie7", "msie 7"),
        /***
         * IE8
         */
        IE8("ie8", "msie 8"),
        /**
         * IE9
         */
        IE9("ie9", "msie 9"),
        /**
         * IE10
         */
        IE10("ie10", "msie 10"),
        /**
         * IE11
         */
        IE11("ie11", "gecko"),
        /**
         * IE
         */
        IE("ie", "msie"),
        /**
         * EDGE
         */
        DEGE("edge","edge"),
        /**
         * OPERA
         */
        OPERA("opera", "opera"),
        /**
         * FIREFOX
         */
        FIREFOX("firefox", "firefox"),
        /**
         * WEBKIT
         */
        WEBKIT("webkit", "webkit"),
        /**
         * OTHERS
         */
        OTHERS("others", "others");

        private String browserName;
        private String browserAgent;

        BrowserTypeEnum(String browserName, String browserAgent) {
            this.browserName = browserName;
            this.browserAgent = browserAgent;
        }

        public String getBrowserName() {
            return browserName;
        }

        public String getBrowserAgent() {
            return browserAgent;
        }
    }
}
